var userInfo = "edit-user-info";
var settings = "edit-settings";
var password = "edit-password-update";

var currentTab = userInfo;
function tabChanged(tabName) {
    currentTab = tabName;
    if (tabName == settings) {
        initSettings();
    }
}

function commitForm() {
    if (currentTab == userInfo) {
        commitUserInfo();
    } else if (currentTab == settings) {
        //commitSettings();
    } else if (currentTab == password) {
        commitPassword();
    }
}

function commitUserInfo() {

}

function commitSettings() {
    if (cochoApp.isNotEmpty(deviceList)) {
        var deviceIds = "";
        for (var i = 0; i < deviceList.length; i++) {
            var deviceInfo = deviceList[i];
            if ($("#" + deviceInfo.deviceId).attr("checked") == "checked") {
                deviceIds += deviceInfo.deviceId + ",";
            }
        }
        deviceIds = deviceIds.substring(0, deviceIds.lastIndexOf(","));

        var paramData = {"deviceIds": deviceIds, "token": cochoApp.getTokenByStorage()};
        $.ajax({
            type: "post",
            async: true,//异步，如果等于false 那么就是同步
            url: cochoApp.baseUrl + "/user/uc/saveUserRelationDevices.do",
            dataType: "json",
            data: paramData,
            success: function (data) {
                if (data != null) {
                    if (data.statusCode == "SUCCESS") {
                        cochoAlert.notificationsWarn("保存成功！");
                    } else {
                        cochoAlert.notificationsWarn(data.statusMsg);
                    }
                }
            },
            error: function (data) {
                cochoAlert.notificationsWarn("保存关联的设备信息出错！");
            }
        });


    } else {
        cochoAlert.notificationsWarn("请联系管理员添加设备信息！");
    }
}

function commitPassword() {
    if(cochoApp.isEmpty($("#form-field-current-password").val())){
        cochoAlert.notificationsWarn("当前密码不能为空！！");
    }else if(cochoApp.isEmpty($("#form-field-password1").val())){
        cochoAlert.notificationsWarn("新密码不能为空！！");
    }else if(cochoApp.isEmpty($("#form-field-password2").val())){
        cochoAlert.notificationsWarn("确认密码不能为空！");
    }else if($("#form-field-password1").val() != $("#form-field-password2").val()){
        cochoAlert.notificationsWarn("两次输入的密码不一致！");
    }else{
        var paramData = {
            "currentPassword": $("#form-field-current-password").val(),
            "password1": $("#form-field-password1").val(),
            "password2": $("#form-field-password2").val(),
            "token": cochoApp.getTokenByStorage()
        };
        $.ajax({
            type: "post",
            async: true,//异步，如果等于false 那么就是同步
            url: cochoApp.baseUrl + "/user/uc/updatePassword.do",
            dataType: "json",
            data: paramData,
            success: function (data) {
                if (data != null) {
                    cochoAlert.notificationsWarn(data.statusMsg);
                }
            },
            error: function (data) {
                cochoAlert.notificationsWarn("修改用户密码出错！");
            }
        });
    }
}

var deviceList;
function initSettings() {
    var paramData = {"token": cochoApp.getTokenByStorage()};
    $.ajax({
        type: "post",
        async: true,//异步，如果等于false 那么就是同步
        url: cochoApp.baseUrl + "/user/uc/getDeviceInfos.do",
        dataType: "json",
        data: paramData,
        success: function (data) {
            if (data != null) {
                if (data.statusCode == "SUCCESS") {
                    updateDeviceInfos(data);
                } else {
                    cochoAlert.notificationsWarn(data.statusMsg);
                }
            }
        },
        error: function (data) {
            cochoAlert.notificationsWarn("获取关联的设备信息出错！");
        }
    });
}

function updateDeviceInfos(data) {
    deviceList = data.dataRows;
    if (cochoApp.isNotEmpty(deviceList)) {
        var deviceHtmlContent = "";
        for (var i = 0; i < deviceList.length; i++) {
            var deviceInfo = deviceList[i];
            var checked = deviceInfo.relationed == true ? "checked" : "";
            deviceHtmlContent += "<div>"
                + " <label class='inline'>"
                + " <input id='" + deviceInfo.deviceId + "' value='" + deviceInfo.deviceId + "' type='checkbox' " + checked + " name='form-field-checkbox' class='ace'/>"
                + "      <span class='lbl'> " + deviceInfo.deviceName + "</span>"
                + " </label>                    "
                + " </div>                     "
                + " <div class='space-8'></div>";
        }
        $(".device_list").html(deviceHtmlContent);
    } else {
        $(".device_list").html("暂无可关联的设备信息，请联系管理员添加设备信息！");
    }
}
